<template>
  <div class="login-contain">
    <el-form ref="form" class="login-form" :model="form" :rules="rules">
      <img src="../../assets/common/login-logo.png" alt="" class="pic" />
      <!-- 手机号码 -->
      <el-form-item class="ipt" prop="mobile">
        <el-input
          prefix-icon="el-icon-user-solid"
          v-model="form.mobile"
          @keyup.enter.native="submit"
        ></el-input>
      </el-form-item>
      <!-- 密码 -->
      <el-form-item class="ipt" prop="password">
        <el-input
          prefix-icon="el-icon-lock"
          type="password"
          v-model="form.password"
          @keyup.enter.native="submit"
        ></el-input>
      </el-form-item>
      <!-- 登录按钮 -->
      <el-form-item>
        <el-button type="primary" class="login-btn" @click="submit"
          >登录</el-button
        >
      </el-form-item>
      <el-form-item>
        帐号：13800000002,密码：123456
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { validatePhone } from '../../utils/validate.js'
import { login } from '../../api/user.js'
export default {
  data () {
    // 自定义规则函数,传入三个参数,规则,校验参数,回调函数
    const valiMobile = (rule, value, callback) => {
      validatePhone(value)
        ? callback()
        : callback(new Error('请正确输入手机号'))
    }
    return {
      form: {
        mobile: '13800000002', //string	必须
        password: '123456' //string	必须
      },
      rules: {
        mobile: [
          { required: true, message: '必填', trigger: 'blur' },
          {
            // 自定义规则
            validator: valiMobile,
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '必填', trigger: 'blur' },
          { min: 6, max: 16, message: '请输入6-16位密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submit () {
      // window.console.log('submit')
      /*
      登陆处理：
      1：定义登陆api
      2:导入登陆api
      3:在表单验证通过后调用登陆api
      4:在登陆成功后存储token并跳转到首页
    
     需要在vuex中调用登陆接口，同时将调用成功返回的token通过vuex进行管理
     1：定义一个模块(user)有一个token变量用于存储token
     2:定义mutations与actions(调用登陆接口获取token)
     3:在点击登陆按钮时调用actions方法实现登陆

      */
      this.$refs.form.validate(async result => {
        if (result) {
          this.$message.success('表单验证成功')
          // 验证成功后调用接口操作交由actions执行,需要加上await修饰符,等待异步函数执行完毕再跳转页面
          await this.$store.dispatch('user/Login', this.form)
          this.$router.push('/')
        } else {
          this.$message.error('表单验证失败')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login-contain {
  height: 100vh;
  background-image: url('../../assets/common/login.jpg');
  background-position: center;
  display: flex;
  align-items: center;
  justify-content: center;
  .login-form {
    text-align: center;
    .ipt {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(255, 255, 255, 0.7); // 输入登录表单的背景色
      border-radius: 5px;
      color: #454545;
    }
    // 图片样式
    .pic {
      margin-bottom: 20px;
    }
    // 登录按钮样式
    .login-btn {
      width: 100%;
      background: #407ffe;
      height: 64px;
      line-height: 32px;
      font-size: 24px;
    }
  }
}
</style>
